package fhp.hlhj.giantfold.adapter

import android.content.Context
import android.support.v4.view.PagerAdapter
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.makeramen.roundedimageview.RoundedImageView
import fhp.hlhj.giantfold.R

/**
 * author: FHP
 * data: on 2017\11\30 0030 16:42
 * E-Mail: 103945089@qq.com
 */
class ShowAnnualViewPagerAdp(private val context: Context) :PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = LayoutInflater.from(context).inflate(R.layout.show_annual_pager_item,null)
        val img = view.findViewOften<RoundedImageView>(R.id.img)
        when(position){
            0->{
                img.setImageResource(R.mipmap.snh_imgf)
            }
            1->{
                img.setImageResource(R.mipmap.snh_imgfy)
            }
            2->{
                img.setImageResource(R.mipmap.snh_imgj)
            }
            3->{
                img.setImageResource(R.mipmap.snh_imgk)
            }
            4->{
                img.setImageResource(R.mipmap.dbj)
            }
            5->{
                img.setImageResource(R.mipmap.snh_imgt)
            }
        }
        container.addView(view)

        return view

    }
    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any?) {
        container.removeView(`object` as View)
    }

    override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
        return view==`object`
    }

    override fun getCount(): Int {
        return 6
    }
    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }
}